﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gpsys.Entity.MSG;
using Gpsys.Logger;


namespace Gpsys.Web.Business
{
    public class AdapterBusiness : BaseBusiness
    {
        public List<string> GetAdapterCodes()
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<string>(@"SELECT Distinct [Code]
                                              FROM [Adapter] ").ToList();

            }
            catch (Exception e)
            {
                Log.WriteError("GetAdapterCodes : " + e.Message);

                return new List<string>();
            }
            finally
            {
                SQL.CloseConnection();
            }
        }


        public Adapter GetAdapter(string code, int type)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Adapter>(@"SELECT [Id]
                                              ,[Code]
                                              ,[Type]
                                              ,[Description]
                                              ,[ClassType]
                                              ,[ConfigurationClassType]
                                              FROM [Adapter] 
                                              WHERE [Code] = @code AND [TYPE] = @type", new { code = code, type = type }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("GetAdapter : " + e.Message);

                return null;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        public List<Adapter> GetAdapters()
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Adapter>(@"SELECT [Id]
                                              ,[Code]
                                              ,[Type]
                                              ,[Description]
                                              ,[ClassType]
                                              ,[ConfigurationClassType]
                                              FROM [Adapter] ").ToList();

            }
            catch (Exception e)
            {
                Log.WriteError("GetAdapters : " + e.Message);

                return new List<Adapter>();
            }
            finally
            {
                SQL.CloseConnection();
            }
        }
    }
}
